import file from '@system.file';
import request from '@ohos.request';
import fetch from '@system.fetch';
import router from '@system.router';
import prompt from '@system.prompt';
import geolocation from '@system.geolocation';
import Service from '../../generated/TestService.js';
import dataStorage from '@ohos.data.storage'
import mediaLibrary from '@ohos.multimedia.mediaLibrary';
import featureAbility from '@ohos.ability.featureAbility';
import http from '@ohos.net.http';
import fileio from '@ohos.fileio';

export default {
    data: {
        userid:null,
        username:null,
        password:null,
        email:null,
        headshot:null,
        sex:null,
        phonenumber:null,
        direction: 'column',
        trips:[{"locations":["113.0,35.0","113.0,35.0"],"tripTime":1646454358000}],
    },
    async onInit(){
        this.context = featureAbility.getContext()
        this.path = await this.context.getFilesDir()
        this.cachepath = await this.context.getCacheDir()

    },
    async onShow(){
        this.context = featureAbility.getContext();
        this.path = await this.context.getFilesDir();
        let storage = dataStorage.getStorageSync(this.path + '/mystore')
        this.userid = storage.getSync('userid','-1');
        this.username = storage.getSync('username', '未登录')
        this.password = storage.getSync('password', '-1')
        this.email = storage.getSync('email', '-1')
        this.headshot = storage.getSync('headshot', '-1')
        this.sex = storage.getSync('sex', '-1')
        this.phonenumber = storage.getSync('phonenumber', '-1')

        this.loadHistoryTrips()
    },
    TurnBack(){
        router.back();
    },
    loadHistoryTrips(){
        var that = this
        if(this.userid!=null && this.userid!='-1'){
            let httpRequest= http.createHttp();
            httpRequest.request("http://101.35.82.76:9002/trip/gettrips?userid="+this.userid,
                {
                    method: 'GET',
                    readTimeout: 60000,
                    connectTimeout: 60000
                },(err, data) => {
                    if (!err) {
                        console.info('Result:' + data.result);
                        console.info('code:' + data.responseCode);
                        console.info('header:' + data.header);

                        var res = JSON.stringify(JSON.parse(data.result).data)
                        var trips = JSON.stringify(JSON.parse(res).trips)
                        that.trips = JSON.parse(trips)

                    } else {
                        console.info('error:' + err.data);
                        prompt.showToast({
                            message: 'LoadHistoryTrips Error'+ JSON.stringify(err)
                        })
                    }
                });
        }
    }
}
